package com.oocl.ita.handler;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import com.oocl.ita.qqclientt.QQClient;
import com.oocl.ita.sakura.client.Client;
import com.oocl.ita.sakura.protocol.Protocol;
import com.oocl.ita.ui.ChatFrame;

public class ChatHandler implements IHandler {


	public void handle(Protocol protocol) {
		/*
		 * 1. 解析Protocol，获取要展示的内容 from to content
		 * 
		 * 2. 刷新JPanel Jtextpanel 追加文本? 使用Document类
		 */

		// debug
		System.out.println("ChatHandler " + QQClient.getClient());
		
		// 获取Protocol内容
		Client fromClient = (Client) protocol.getParams().get("from");
		Client toClient = (Client) protocol.getParams().get("to");
		String content = (String) protocol.getParams().get("content");

		// 刷新JPanel
		Document docs = ChatFrame.getContentTextPane().getDocument();// 获得文本对象
		SimpleAttributeSet attrset = new SimpleAttributeSet();
		StyleConstants.setFontSize(attrset, 12);
		try {
			String time = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date());
			String chatRecord = "\nfrom " + fromClient.getAccout() + " to " + toClient.getAccout()  + " at " + time + "\n" + content;
			docs.insertString(docs.getLength(), chatRecord, attrset);
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 对文本进行追加
	}

}
